package com.lgs.demo.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.lgs.demo.R;
import com.lgs.demo.base.BaseLazyFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lgs on 2018/1/28.
 */

public class ConnectFragment extends BaseLazyFragment {

    @BindView(R.id.listView)
    ListView mListView;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_connect;
    }

    public static ConnectFragment newInstance() {
        return new ConnectFragment();
    }

    @Override
    protected void init() {
        super.init();
        initData();
    }

    private List<String> list;

    private void initData() {
        list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add("Android & Java" + i);
        }
        mListView.setAdapter(new MyAdapter(getContext()));
    }

    public class MyAdapter extends BaseAdapter {

        private LayoutInflater mInflater;

        public MyAdapter(Context context) {
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (null == convertView) {
//                convertView = View.inflate(getActivity(), R.layout.adapter_item, parent);
                convertView = mInflater.inflate(R.layout.adapter_item, parent, false);
                holder = new ViewHolder(convertView);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "内容区域", Toast.LENGTH_SHORT).show();
                }
            });
            holder.right.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "滑动区域", Toast.LENGTH_SHORT).show();
                }
            });

            return convertView;
        }

        class ViewHolder {

            @BindView(R.id.content)
            LinearLayout content;

            @BindView(R.id.right)
            LinearLayout right;

            public ViewHolder(View convertView) {
                ButterKnife.bind(this, convertView);
                convertView.setTag(this);
            }
        }
    }
}
